package org.example;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * @author xhw
 */
public class test15 {

    public static void main(String[] args) {
        // 1.创建一个类
        Person p = new Person("a1", "小花", "123");

        // 2.创建文件
        String property = System.getProperty("user.home");
        File folder = new File(property, "/my_java_test");
        folder.mkdir(); // 如果存在就不会继续创建
        File file = new File(folder, "/test2.txt");

        try (
                // 3.创建一个字节输出输入流
                FileOutputStream fos = new FileOutputStream(file);
                ObjectOutputStream oos = new ObjectOutputStream(fos);

                // 5.创建一个字节输入流
                FileInputStream fis = new FileInputStream(file);
                ObjectInputStream ois = new ObjectInputStream(fis);
        ) {

            // 4.将对象写入输出流进行序列化
            oos.writeObject(p);
            System.out.println(oos);

            // 6.从输入流读取对象进行反序列化
            Person person = (Person) ois.readObject();
            System.out.println("Person: " + person);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Person implements Serializable {
    private String account;
    private String name;
    private transient String password;

    private Boolean pro1;
    private boolean pro2;

    public Person(String account, String name, String password) {
        this.account = account;
        this.name = name;
        this.password = password;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "Person{" +
                "account='" + account + '\'' +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}
